package su.demo.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import su.demo.annotaition.DataSource;
import su.demo.config.datasourceConfigs.DataSourceContextHolder;

/**
 * @author SuZuQi
 * @title: DataSourceAspect
 * @projectName ssm_multipledatasources
 * @description: TODO
 * @date 2019/9/6
 */
@Component
@Aspect
@Order(-1)
public class DataSourceAspect {

    @Pointcut("@within(su.demo.annotaition.DataSource) || @annotation(su.demo.annotaition.DataSource)")
    public void pointCut(){}


    @Before("pointCut() && @annotation(dataSource)")
    public void doBefore(DataSource dataSource){
        DataSourceContextHolder.setDataSource(dataSource.value().getValue());
    }


    @After("pointCut()")
    public void doAfter(){
        DataSourceContextHolder.clear();
    }
}
